import 'dart:async';

import 'package:bookshelf/main.dart';
import 'package:get/get.dart';


class Saver {

  Saver._internal();

  static final Saver _singleton = Saver._internal();

  factory Saver() => _singleton;

  Timer? _timer;
  Timer? _indexTimer;


  RxBool isSleep = RxBool(false);
  RxInt index = RxInt(1);


  void start(){
    cancel();

    _timer = Timer.periodic(Duration(seconds: settingEntity.sleepTime??60), (timer) {
      if(Get.isDialogOpen != null && Get.isDialogOpen!){
        return;
      }
      isSleep.value = true;
    });
    _indexTimer = Timer.periodic(const Duration(seconds: 10), (timer) {
      index.value = index.value +1;
    });
  }

  void cancel(){
    if(_timer != null){
      if (_timer!.isActive) {
        _timer!.cancel();
        _timer = null;
      }
    }
    if(_indexTimer != null){
      if (_indexTimer!.isActive) {
        _indexTimer!.cancel();
        _indexTimer = null;
      }
    }
  }

}
